import 'package:demo/common/controller/app_ctrl.dart';
import 'package:demo/common/i18n/my_translation.dart';
import 'package:demo/common/utils/dialog_util.dart';
import 'package:demo/common/utils/log_util.dart';
import 'package:demo/common/widgets/no_shadow_scroll_behavior.dart';
import 'package:demo/common/widgets/page/page_ctrl.dart';
import 'package:demo/common/widgets/state/state_config.dart';
import 'package:demo/config/config.dart';
import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';

import 'common/routers/route_names.dart';
import 'common/routers/route_pages.dart';
import 'common/utils/sp_util.dart';

Future<void> main() async {
  await _init();
  runApp(const MyApp());
}

Future<void> _init() async {
  // 确保在Flutter,已经原生接口初始化完成之后再初始化
  WidgetsFlutterBinding.ensureInitialized();

  // 初始化shared_preferences
  SpUtil().init();

  //基础配置
  await Config().init();

  // dialog默认值
  DialogUtil.init();

  // 全局控制器
  Get.put(AppCtrl(), permanent: true);

  /// 配置全局缺省页
  StateConfig.buildEmpty = (it) {
    return Text("empty");
  };

  StateConfig.buildError = (it, onTapRetry) {
    return Text("Error");
  };
  StateConfig.buildLoading = (it) {
    return Container(
      color: Colors.black,
      child: Center(child: Text("Loading")),
    );
  };

  /// 刷新Header和Footer
  EasyRefresh.defaultHeaderBuilder = () => MaterialHeader();
  EasyRefresh.defaultFooterBuilder = () => CupertinoFooter(emptyWidget: Text("没有更多了"));

  /// 默认起始页码
  PageCtrl.defaultStartIndex = 1;
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit(
      designSize: const Size(375, 811),
      builder: (context, child) {
        return buildMaterialApp();
      },
    );
  }

  Widget buildMaterialApp() {
    return GetMaterialApp(
      // 应用标题
      title: 'demo',
      // 不显示调试横幅
      debugShowCheckedModeBanner: false,
      // 初始路由名称
      initialRoute: RouteNames.launch,
      // 路由页面列表
      getPages: RoutePages.list,
      //路由监听
      navigatorObservers: [FlutterSmartDialog.observer, Log.observer],
      // 多语言
      translations: MyTranslation(),
      // 代理
      localizationsDelegates: MyTranslation.localizationsDelegates,
      // 支持的语言种类
      supportedLocales: MyTranslation.supportedLang,
      // 当前语言种类
      locale: Config().locale,
      // 默认语言种类
      fallbackLocale: MyTranslation.defaultLocal,
      builder: (context, child) {
        // 初始化
        child = FlutterSmartDialog.init()(context, child);
        child = EasyLoading.init()(context, child);

        // 配置 MediaQuery 和 ScrollConfiguration
        return MediaQuery(
          data: MediaQuery.of(context).copyWith(textScaler: const TextScaler.linear(1.0)), // 设置文本缩放比例为1.0
          child: ScrollConfiguration(
            behavior: NoShadowScrollBehavior().copyWith(
              overscroll: false, // 禁用水波纹效果
            ),
            child: child,
          ),
        );
      },
    );
  }
}
